<?php

namespace App\Http\Controllers;

use App\Models\ShortUrl;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserVideoRequest;
use App\Models\Site;
use App\Models\Domain;
use App\Handlers\ShortUrlHandler;
use App\Models\UserVideo;
use Auth;
use Illuminate\Support\Str;
use App\Handlers\ShareUrlHandler;
class ShortUrlsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

	public function index()
	{
		$short_urls = ShortUrl::paginate();
		return view('short_urls.index', compact('short_urls'));
	}

    public function show(Site $site, Request $request)
    {
        $this->authorize('destroy', $site);
        $user = Auth::user();
        if ($user->hasRole('Founder')) $sites = Site::all();
        else $sites = $user->sites;
        if ($request->has('type')) {
            $create = $request->type;
        } else {
            $create = 1;
        }

        return view('short_urls.show', compact('user', 'sites', 'site', 'create'));
    }
    public function create_fixed(Site $site, UserVideo $user_video, ShortUrlHandler $short, UserVideoRequest $request, ShareUrlHandler $share)
    {
        $user = $request->user();
        $site_id = $request->input('siteid', 0);
        $link_type = $request->input('link_type', 1);
        if($site_id > 0) $site = Site::find($site_id);
        $this->authorize('destroy', $site);
        $notVideos = ['status' => 422, 'message' => '请填写视频地址'];
        $video_data = $request->input('video_urls','');
        if(empty($video_data)) return $notVideos;

        $videos = collect($video_data);

        $user_videos = $videos->map(function ($value)
            use ($user_video, $user, $site)
            {
                return $user_video->firstOrCreate(['user_id' => $user->id, 'site_id' => $site->id, 'create_type' => 1, 'link' => $value]);
            });
        // 根据 user_videos 生成地址
        $domains = Domain::all();
        // 生成原始地址
        $urls = $user_videos->map(function($value)
            use ($domains, $site) {
                $domain = $domains->random()->domain;
                Str::startsWith($domain, ['http://', 'https://']) ? $http = '' : $http = 'http://';
                return $http . $domain . '?site=' . $site->id . '&user_id=' . $site->user_id . '&video=' . $value->id . '&str=' . str_random(32);
        });
        if ($link_type > 2) {
          $short_urls = $short->url2short($urls, 'w_url');
          $share->get($user->id, ['type' => 3, 'urls' => $short_urls]);
          return ['datas' => $short_urls];
        }
        else if ($link_type > 1){
            $share->get($user->id, ['type' => 1, 'urls' => $urls]);
            return ['datas' => $urls];
        } else {
            $short_urls = $short->url2short($urls);
            $share->get($user->id, ['type' => 2, 'urls' => $short_urls]);
            return ['datas' => $short_urls];
        }
    }
    public function create_random(Site $site, UserVideo $user_video, ShortUrlHandler $short, UserVideoRequest $request, ShareUrlHandler $share)
    {
        $user = $request->user();
        $site_id = $request->input('siteid', 0);
        $link_type = $request->input('link_type', 1);
        if ($site_id > 0) $site = Site::find($site_id);
        $this->authorize('destroy', $site);
        $notVideos = ['status' => 422, 'message' => '请填写视频地址'];
        $video_data = $request->input('video_urls','');
        $update_history = $request->input('update_history', 0);
        $nums = $request->input('nums', 1);
        $videos = '';
        //if (!empty($video_data)) {
            $video_new_data = collect($video_data)->filter();
        //}
        if (empty($video_new_data->all())) {
            if ($update_history) return $notVideos;
            $user_videos = UserVideo::where('user_id', $user->id)
                ->where('site_id', $site->id)
                ->where('create_type', 2)
                ->pluck('id')->toArray();
                if (empty($user_videos)) {
                    $user_videos = [0];
                    //return $notVideos;
                }
        } else {
            // 更新历史地址

            if ($update_history) {
                $user_video
                    ->where('user_id', $user->id)
                    ->where('site_id', $site->id)
                    ->where('create_type', 2)
                    ->each(function($video) use ($video_new_data){
                        $video->link = $video_new_data->random();
                        $video->random_id = 1;
                        $video->save();
                    });
                $random_id = 1;
            } else {
                $random_id = $user_video->where('user_id', $user->id)
                    ->where('site_id', $site->id)
                    ->where('create_type', 2)
                    ->max('random_id');
                $random_id++;
            }

            $user_videos = $video_new_data->map(function ($value)
            use ($user_video, $user, $site, $random_id)
            {
                if( !empty($value)) {
                    return $user_video->firstOrCreate(['user_id' => $user->id, 'site_id' => $site->id, 'create_type' => 2, 'link' => $value, 'random_id' => $random_id]);
                }
            })->filter()->pluck('id')->toArray();
        }
        // 根据 user_videos 生成地址
        $domains = Domain::all();
        // 按条数生成URL
        $videos = collect($user_videos);
        for ($i = 0;$i < $nums; $i++) {
            $domain = $domains->random()->domain;
            Str::startsWith($domain, ['http://', 'https://']) ? $http = '' : $http = 'http://';


            $video_id = $videos->random();
            $urls[] = $http . $domain . '?site=' . $site->id . '&user_id=' . $site->user_id . '&video=' . $video_id . '&str=' . str_random(32);
        }
        if ($link_type > 2) {
          $short_urls = $short->url2short($urls, 'w_url');
          $share->get($user->id, ['type' => 3, 'urls' => $short_urls]);
          return ['datas' => $short_urls];
        } else if ($link_type > 1){
            $share->get($user->id, ['type' => 1, 'urls' => $urls]);
            return ['datas' => $urls];
        } else {
            $short_urls = $short->url2short($urls);
            $share->get($user->id, ['type' => 2, 'urls' => $short_urls]);
            return ['datas' => $short_urls];
        }
    }
    public function share(ShareUrlHandler $share)
    {
        $user = Auth::user();


        $data = $share->get($user->id);
        if(!$data) return back()->with('warning', '数据不存在！');
        return view('short_urls.share', compact('data'));
    }
}
